package com.example.home.library;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LibraryMainActivity extends AppCompatActivity {

    private Button next_page;
    private Button previous;
    private Request request;
    private LibraryBean libraryBean;
    private Handler handler;
    private int pageNum = 1;
    private OkHttpClient okHttpClient;
    private URL url;
    private RecyclerView re_library;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library_main);

        initView();
        librarybean(url);
        previousBean();
        NextBean();
    }

    private void librarybean(URL url) {
        request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", OKHttpTool.TOKEN)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                Log.d("getHotDate", json);
                System.out.println("json" + json);
                Gson gson = new Gson();
                libraryBean = gson.fromJson(json, LibraryBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        binlibrarydate();
                    }
                });
            }
        });
    }

    private void binlibrarydate() {
        re_library = findViewById(R.id.re_library);
        LibraryAdapter libraryAdapter = new LibraryAdapter(this, libraryBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        re_library.setLayoutManager(linearLayoutManager);
        re_library.setAdapter(libraryAdapter);
    }

    private void previousBean() {
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum--;
                try {
                    url = new URL("http://124.93.196.45:10001/prod-api/api/digital-library/library/list?pageNum=" + pageNum + "+&pageSize=10");
                    librarybean(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void NextBean() {
        next_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum++;
                try {
                    url = new URL("http://124.93.196.45:10001/prod-api/api/digital-library/library/list?pageNum=" + pageNum + "+&pageSize=10");
                    librarybean(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initView() {
        handler = new Handler();
        next_page = findViewById(R.id.next_page);
        previous = findViewById(R.id.Previous);

        okHttpClient = new OkHttpClient(); 

        try {
            url = new URL("http://124.93.196.45:10001/prod-api/api/digital-library/library/list?pageNum=" + pageNum + "+&pageSize=10");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
